package net.trevize.datasetexplorer;

import java.awt.BorderLayout;
import java.io.File;
import java.util.regex.Pattern;

import javax.swing.JCheckBox;
import javax.swing.JPanel;

import net.trevize.FSExplorer.FSTree.FSTree;
import net.trevize.galatee.Galatee;
import net.trevize.galatee.GalateeFactory;
import net.trevize.gui.FSExplorer.FSE.FSEEvent;
import net.trevize.gui.FSExplorer.FSE.FSEListener;

public class FSExplorerPanel extends JPanel implements FSEListener {

	private DatasetExplorer dataset_explorer;
	private FSTree fstree;
	private JCheckBox checkbox_recursive;

	public FSExplorerPanel(DatasetExplorer dataset_explorer) {
		this.dataset_explorer = dataset_explorer;
		init();
	}

	private void init() {
		setLayout(new BorderLayout());

		JPanel main_panel = new JPanel();
		main_panel.setLayout(new BorderLayout());
		add(main_panel, BorderLayout.CENTER);

		//String file_filter = "(^[^\\.]$|^[^\\.].*[^~]$)";
		//String dir_filter = "^[^\\.].*$";
		
		String file_filter = "";
		String dir_filter = "";
		
		fstree = new FSTree(File.listRoots()[0], Pattern.compile(file_filter),
				Pattern.compile(dir_filter), true);
		fstree.addFSEListener(this);

		checkbox_recursive = new JCheckBox("recursive directory listing");
		main_panel.add(fstree, BorderLayout.CENTER);
		main_panel.add(checkbox_recursive, BorderLayout.SOUTH);
	}

	/***************************************************************************
	 * implementation of FSEListener
	 **************************************************************************/

	@Override
	public void focusChanged(FSEEvent e) {
	}

	@Override
	public void open(FSEEvent e) {
		Galatee galatee = GalateeFactory.loadDatasetFromDirectory(e
				.getSelectedFile().getPath(), checkbox_recursive.isSelected());
		galatee.addGalateeListener(new GListenerImpl());

		galatee.enableSearchFunctionality();

		GalateePopupMenuInitializer.initialize(galatee, dataset_explorer);

		checkbox_recursive.setSelected(false);

		galatee.requestFocus();

		dataset_explorer.addFSExplorerTab(galatee, e.getSelectedFile()
				.getName());
	}

	@Override
	public void pathChanged(FSEEvent e) {
	}

	@Override
	public void selectionChanged(FSEEvent e) {
	}

}
